
package com.example.myapplication.house;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.HouseDetailsBean;
import com.example.myapplication.ShuJu.StationDetailsBean;
import com.example.myapplication.teenager.StationDetailsActivity;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HouseDetailsActivity extends AppCompatActivity {

    private String searchhousingBeanID;
    private ImageView details_img;
    private TextView details_text;
    private TextView details_mianji;
    private TextView details_type;
    private TextView details_price;
    private TextView details_centent;
    private Button details_bloak;
    private OkHttpClient okHttpClient;
    private Request request;
    private Handler handler;
    private HouseDetailsBean houseDetailsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_details);
        Intent intent = getIntent();
        searchhousingBeanID = intent.getStringExtra("SearchhousingBeanID");
        initview();
        //获取房子的详细信息
        sendhhouse();
    }

    private void sendhhouse() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/house/housing/"+searchhousingBeanID)
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                Gson gson = new Gson();
                houseDetailsBean = gson.fromJson(string,  HouseDetailsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(HouseDetailsActivity.this).load(TokenBean.IP+houseDetailsBean.getData().getPic()).into(details_img);
                        details_text.setText(houseDetailsBean.getData().getSourceName());
                        details_img.setScaleType(ImageView.ScaleType.FIT_XY);
                        details_mianji.setText("建筑面积:"+houseDetailsBean.getData().getAreaSize()+"m");
                        details_price.setText("房源单价:"+houseDetailsBean.getData().getPrice());
                        details_type.setText("房源类型:"+houseDetailsBean.getData().getHouseType());
                        //Log.e("details_centent",);
                        details_centent.setText("房源介绍:"+houseDetailsBean.getData().getDescription());
                        details_bloak.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
//                                Intent intent = new Intent(HouseDetailsActivity.this,HouseActivity.class);
//                                intent.putExtra("HouseDetailsActivityType",houseDetailsBean.getData().getHouseType());
//                                HouseDetailsActivity.this.startActivity(intent);

                               /* Intent intent = new Intent();
                                intent.putExtra("HouseDetailsActivityType",houseDetailsBean.getData().getHouseType());
                                HouseDetailsActivity.this.setResult(HouseDetailsActivity.this.RESULT_OK,intent);
                                HouseDetailsActivity.this.finish();*/

                                //HouseDetailsActivity.this.finish();
                                onBackPressed();
                            }
                        });
                    }
                });
            }
        });
    }

    private void initview() {
        details_img = findViewById(R.id.details_img);
        details_text = findViewById(R.id.details_text);
        details_mianji = findViewById(R.id.details_mianji);
        details_price = findViewById(R.id.details_price);
        details_type = findViewById(R.id.details_type);
        details_centent = findViewById(R.id.details_centent);
        details_bloak = findViewById(R.id.details_bloak);


        okHttpClient = new OkHttpClient();
        handler = new Handler();
    }
}